package com.ws.mbp.springsecuritydemo.bean;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Data
@TableName("users")
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "用户信息")    //@ApiModel ----- @Schema
public class Users implements UserDetails {
    @TableId(type = IdType.AUTO)
    @Schema(description = "用户ID")        //@ApiModelProperty ----- @Schema
    private Integer userId;
    @Schema(description = "用户名")
    private String userName;
    @Schema(description = "用户密码")
    private String userPass;
    @Schema(description = "用户性别")
    private String userSex;
    @Schema(description = "用户年龄")
    private String userAge;
    //数据库没有的字段
    @TableField(exist = false)
    private List<String> roles = new ArrayList<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
       //返回格式：[teacher, student]
        return roles.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
    @Override
    public String getPassword() {
        return userPass;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    /**
     * 账户是否没有过期
     * @return true  表示未过期  false 表示过期
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    /**
     * 账户是否没有被锁
     * @return true  表示未锁  false  表示锁
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    /**
     * 密码是否没有过期
     * @return true 表示未过期  false 表示过期
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账户是否可用
     * @return true 表示可用  false 表示不可用
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
